package com.lzkj.baotouhousingfund.common;

import android.text.TextUtils;

import com.lzkj.baotouhousingfund.base.BaseView;
import com.lzkj.baotouhousingfund.http.exception.ApiException;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import retrofit2.HttpException;

/**
 * Created by codeest on 2017/2/23.
 */

public abstract class CommonConumer<T> implements Consumer<T> {
    private BaseView mView;
    private String   mErrorMsg;

    protected CommonConumer(BaseView view) {
        this.mView = view;
    }

    protected CommonConumer(BaseView view, String errorMsg) {
        this.mView = view;
        this.mErrorMsg = errorMsg;
    }

    @Override
    public void accept(@NonNull T t) {
        try {

        } catch (Exception e) {
            onError(e);
        }
    }

    public void onError(Throwable e) {
        if (mView == null){
            return;
        }
        if (mErrorMsg != null && !TextUtils.isEmpty(mErrorMsg)) {
            mView.showError(mErrorMsg);
        } else if (e instanceof ApiException) {
            mView.showError(e.toString());
        } else if (e instanceof HttpException) {
            mView.showError("数据加载失败ヽ(≧Д≦)ノ");
        } else {
            mView.showError("未知错误ヽ(≧Д≦)ノ");
        }
    }
}
